#include "runshop.h"
#include "QDateTime"
#include "runshop.h"
#include "QLineEdit"
#include "utils/deviceutils.h"
#include "QRunnable"
#include "utils/gamemanager.h"
#include "utils/runshopgoodsmanager.h"


RunShop::RunShop(QWidget *widget):GamePersent(widget){
    setAutoDelete(false); //QRunable的方法默认run方法执行完后会自动销毁对象
    thread = new QThread();
    moveToThread(thread);
    connect(thread, &QThread::started, this, &RunShop::checkMove);
    thread->start();
}

void RunShop::initData(QList<RoleBean*> *roleBean)
{
    this->roleBeans = roleBean;
}

void RunShop::callBack(QString str)
{
    DeviceUtils::sendLog(widget, str);
}

bool RunShop::isCancelTask()
{
    return GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL;
}

//去钱为一 财政官员
void RunShop::goQianWeiYi(){
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    DeviceUtils::sendLog(widget, "前往钱为一");
    int x = GameManager::getInstantce()->currentRole->runshopBean->groupLocationX;
    int y = GameManager::getInstantce()->currentRole->runshopBean->groupLocationY;
    checkCityNode = new CityNode("钱为一","qianweiyi", x, y);
    moveRoles(x, y, this);
    clickNPC(":/runshop/icon/runshop/runshop_qianweiyi.png");
    delete checkCityNode;
}

void RunShop::leave()
{
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    QList<CityNode*> leaveCityNode =  GameManager::getInstantce()->currentRole->runshopBean->leaveLine;
    for(int i = 0 ; i < leaveCityNode.size() ; i++){
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL){
            return;
        }
        addBlood();
        QThread::msleep(TimeManager::getInstance()->getDelay());
        addMagic();
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
        CityNode *nextCity = leaveCityNode.at(i);
        checkCityNode = nextCity;
        qDebug() <<"准备前往"<< nextCity->name;
        DeviceUtils::sendLog(widget, QString("准备前往%1 %2,%3").arg(nextCity->name).arg(nextCity->x).arg(nextCity->y));
        moveRoles(nextCity, this);
        QThread::msleep(TimeManager::getInstance()->getDelay());
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
        if(ImgHelper::getGameMatchPoint(":/runshop/icon/button_sure.png",Scalar(22, 76, 90), Scalar(26, 159, 255),0.7f, QRectF(0.2f, 0.2f, 0.8f, 0.8f), &qRectF)){
            DeviceUtils::sendLog(widget, "点击确定按钮");
            Win32Util::moveMouse((qRectF.left() + qRectF.right())/2, (qRectF.top() + qRectF.bottom())/2);
            QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
            if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
            Win32Util::mouseLeftClick();
        }

        while(!getMapString().contains(nextCity->enName) && nextCity->enName != XIAO_WEI){
            QThread::msleep(100);
            if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
        }
        QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->scene));
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;

    }
}

//去乔致广
void RunShop::goRemoteMerchant(){
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    DeviceUtils::sendLog(widget, QString("前往商人"));
    int x = GameManager::getInstantce()->currentRole->runshopBean->remoteLocationX;
    int y = GameManager::getInstantce()->currentRole->runshopBean->remoteLocationY;
    checkCityNode = new CityNode("商人","shangren", x, y);
    moveRoles(x, y, this);
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    clickNPC(":/runshop/icon/runshop/runshop_qiaozhiguang.png");
    delete checkCityNode;
}


void RunShop::back()
{
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    QList<CityNode*> backCityNode =  GameManager::getInstantce()->currentRole->runshopBean->backLine;
    int citySize = backCityNode.size();
    QString xiaoweiEnName = backCityNode.at(citySize - 1)->enName; //校尉
    for(int i = 0 ; i< backCityNode.size() ; i++){
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL){
            return;
        }
        addBlood();
        CityNode *nextCity = backCityNode.at(i);
        checkCityNode = nextCity;
        QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
        addMagic();
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
        DeviceUtils::sendLog(widget, QString("准备前往%1 %2,%3").arg(nextCity->name).arg(nextCity->x).arg(nextCity->y));
        qDebug() <<"准备前往"<< nextCity->name;
        moveRoles(nextCity, this);
        QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
        if(ImgHelper::getGameMatchPoint(":/runshop/icon/button_sure.png", Scalar(22, 76, 90), Scalar(26, 159, 255),0.7f,QRectF(0.2f, 0.2f, 0.8f, 0.8f), &qRectF)){
            DeviceUtils::sendLog(widget, "点击确定按钮");
            Win32Util::moveMouse((qRectF.left() + qRectF.right()) / 2, (qRectF.top() + qRectF.bottom())/2);
            QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
            if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
            Win32Util::mouseLeftClick();
        }

        if(nextCity->enName != xiaoweiEnName){

            while(!getMapString().contains(nextCity->enName) ){
                QThread::msleep(100);
                if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
            }
            QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->scene));
            if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
        }
    }

}

void RunShop::openShop(QString img)
{
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    if(!ImgHelper::getGameMatchPoint(img, Scalar(12, 163, 226),Scalar(23, 255, 255), 0.5f, QRectF(0,0.1f,0.4f,0.9f),&qRectF) || GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL){
        qDebug() << "未找到打开商店位置";
        return;
    }
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    Win32Util::moveMouse((qRectF.left() + qRectF.right()) / 2, (qRectF.top() + qRectF.bottom())/2);
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset) + 300);
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    Win32Util::mouseLeftClick();  //点击 打开商店
    DeviceUtils::sendLog(widget, "点击 打开商店");
}

void RunShop::buyGoods(RunShopBean *runshop,bool isGroup, float threshold)
{
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    RunShopGoods *goods;
    if(isGroup){
        goods = RunShopGoodsManager::getInstance()->getGoods("粮食");
    }else{
        goods = RunShopGoodsManager::getInstance()->getGoods(runshop->remoteBuyGoods);
    }

flag:
    do{
        openShop(":/runshop/icon/runshop_shop.png"); //打开商店
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
        if(checkGoodsTime(runshop->time, goods)) break;
        DeviceUtils::sendLog(widget, QString("当前目标%1买价不是最大值， 15秒后将自动刷新价格").arg(goods->name));
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
        QThread::msleep(15000);
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    }while(true);
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(isGroup){
        if(!ImgHelper::getGameMatchPoint(runshop->groupBuyShopImg,Scalar(0, 0, 100), Scalar(0, 0, 255), threshold, QRectF(0.2f, 0.1f, 0.8f, 0.9f), &qRectF) ){
            return;
        }
    }else{
        if(!ImgHelper::getGameMatchPoint(runshop->remoteBuyShopImg,Scalar(0, 0, 100), Scalar(0, 0, 255), threshold, QRectF(0.2f, 0.1f, 0.8f, 0.9f), &qRectF)){
            return;
        }
    }

    int i = 0;
    while(true){
        if(!checkHasGoods(goods->noImg, goods->noStartScalar, goods->noEndScalar)) goto flag;
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
        QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
        Win32Util::moveMouse(qRectF.left(), (qRectF.top() + qRectF.bottom())/2);
        QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
        Win32Util::mouseLeftClick();  //
        QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
        if(checkOperationFast() || checkContentChange() || !checkGoodsTime(runshop->time, goods)){
            goto flag;
        }
        if(checkBuy() || checkHasYinPiao() || checkHasRunshopLine()){
            break;
        }

        i++;
        if(isGroup){
            DeviceUtils::sendLog(widget, QString("购买粮食%1次").arg(i));
        }else{
            DeviceUtils::sendLog(widget, QString("购买%1%2次").arg(runshop->remoteBuyGoods).arg(i));
        }
    }
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    Win32Util::keyboardClick(VK_ESCAPE);
}

/**
 * 在自己帮派出卖东西
 * @brief RunShop::groupSell
 * @param openShopImg
 */
void RunShop::groupSell(QString openShopImg)
{
    RoleBean *roleBean = GameManager::getInstantce()->currentRole;
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL)return;
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    if(!ImgHelper::getGameMatchPoint(":/runshop/icon/package.png",Scalar(26, 134, 250),Scalar(29, 137, 253),Setting::getInstance()->getImgDiscern(),QRectF(0.6f,0.1f,1,0.6f), &qRectF) || GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL){
        return;
    }
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    Win32Util::moveMouse((qRectF.left() + qRectF.right()) / 2, qRectF.bottom() - 10);
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    Win32Util::mouseLeftClick();  //点击 材料
    DeviceUtils::sendLog(widget, "点击 材料");
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    Win32Util::moveMouse(460,450);
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    QList<QSharedPointer<QRectF>> rectfs;
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL || !ImgHelper::getGameMatchMultiPoint(roleBean->runshopBean->remoteSellShopImg, roleBean->runshopBean->remoteStartScalar,roleBean->runshopBean->remoteEndScalar, 0.7f ,&rectfs,  QRectF(0.5f, 0.1f, 1, 1))){
        DeviceUtils::sendLog(widget, "未识别到需要出售的" + roleBean->runshopBean->remoteBuyGoods);
        return;
    }
    if(rectfs.isEmpty()) return;
    do{
        if(roleBean->runshopBean->remoteCityEn == "dali" || roleBean->runshopBean->remoteCityEn == "luoyang" || roleBean->runshopBean->remoteCityEn == "suzhou"){
            break;
        }
        int gameTime = getGameTime(); //当时辰为丑、辰、未、戌时，粮食的买价和卖价
        QStringList currentTimes = RunShopGoodsManager::getInstance()->getBestTime(gameTime);
        if(currentTimes.contains(roleBean->runshopBean->time)){
            DeviceUtils::sendLog(widget, QString("找到当前粮食最高价"));
            break;
        }
        DeviceUtils::sendLog(widget, QString("当前%1价格不为最高价，15秒后将自动刷新价格").arg(roleBean->runshopBean->remoteBuyGoods));
        QThread::msleep(15000);
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    }while(true);
    openShop(openShopImg); //打开商店

    DeviceUtils::sendLog(widget, QString("识别到出售%1数量为%2").arg(roleBean->runshopBean->remoteBuyGoods).arg(rectfs.size()));
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL || rectfs.isEmpty()) return;

    for(int i =0; i < rectfs.size() ; i++){
        QSharedPointer<QRectF> rectf = rectfs.at(i);
        QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
        Win32Util::moveMouse(rectf->left() + 5, rectf->bottom() - 5);
        QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
        Win32Util::mouseRightClick();
        DeviceUtils::sendLog(widget, QString("出售第%1个%2").arg(i + 1).arg(roleBean->runshopBean->remoteBuyGoods));
    }
}

/**
 *在对方帮派出卖东西
 * @brief RunShop::remoteSell
 * @param openShopImg
 */
void RunShop::remoteSell(QString openShopImg)
{

    RoleBean *roleBean = GameManager::getInstantce()->currentRole;
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL)return;
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    if(!ImgHelper::getGameMatchPoint(":/runshop/icon/package.png",Scalar(26, 134, 250),Scalar(29, 137, 253),Setting::getInstance()->getImgDiscern(),QRectF(0.6f,0.1f,1,0.6f), &qRectF) || GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL){
        return;
    }
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    Win32Util::moveMouse((qRectF.left() + qRectF.right()) / 2, qRectF.bottom() - 10);
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    Win32Util::mouseLeftClick();  //点击 材料
    DeviceUtils::sendLog(widget, "点击 材料");
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    Win32Util::moveMouse(460,450);
    QList<QSharedPointer<QRectF>> rectfs;
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL || !ImgHelper::getGameMatchMultiPoint(roleBean->runshopBean->groupSellShopImg, roleBean->runshopBean->groupStartScalar,roleBean->runshopBean->groupEndScalar, 0.7f ,&rectfs,  QRectF(0.5f, 0.1f, 1, 1))){
        DeviceUtils::sendLog(widget, "未识别到需要出售的粮食");
        return;
    }
    if(rectfs.isEmpty())return;
    do{

        int gameTime = getGameTime(); //当时辰为丑、辰、未、戌时，粮食的买价和卖价
        QStringList currentTimes = RunShopGoodsManager::getInstance()->getBestTime(gameTime);
        if(currentTimes.contains(roleBean->runshopBean->time)){
            DeviceUtils::sendLog(widget, QString("找到当前粮食最高价"));
            break;
        }
        DeviceUtils::sendLog(widget, "当前粮食价格不为最高价，15秒后将自动刷价格");
        QThread::msleep(15000);
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    }while(true);

    openShop(openShopImg); //打开商店
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;

    DeviceUtils::sendLog(widget, QString("识别到出售的商品粮食数量为%1").arg(rectfs.size()));
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL || rectfs.isEmpty()) return;
    for(int i =0; i < rectfs.size() ; i++){
        QSharedPointer<QRectF> rectf = rectfs.at(i);
        QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
        Win32Util::moveMouse(rectf->left() + 5, rectf->bottom() - 5);
        QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
        Win32Util::mouseRightClick();  //点击 粮食
        DeviceUtils::sendLog(widget, QString("出售第%1个粮食").arg(i + 1));
    }
}


void RunShop::buyCity(QString img,Scalar start, Scalar end, float threshold)
{
flag:
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(!ImgHelper::getGameMatchPoint(img,start, end, threshold, QRectF(0.2f, 0.1f, 0.8f, 0.9f), &qRectF) || GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL){
        return;
    }
    int i = 0;
    while(true){
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
        QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
        Win32Util::moveMouse(qRectF.left() + 20 , (qRectF.top() + qRectF.bottom())/2);
        QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset) +500);
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
        Win32Util::mouseLeftClick();
        QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
        if(checkBuy()){
            break;
        }
        if(checkOperationFast() || checkContentChange()){
            openShop(":/runshop/icon/runshop/runshop_open.png");
            goto flag;
        }
        i++;
        DeviceUtils::sendLog(widget, QString("购买商品%1次").arg(i));
    }
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    Win32Util::keyboardClick(VK_ESCAPE);

}


//领取银票
void RunShop::getBankNotes()
{
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    if(!ImgHelper::getGameMatchPoint(":/runshop/icon/runshop/runshop_yinpiao.png", Scalar(14, 125, 84), Scalar(24, 175, 255), 0.6f, QRectF(0, 0.2f, 0.3f,0.8f), &qRectF) || GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL){
        return;
    }
    Win32Util::moveMouse((qRectF.left() + qRectF.right()) / 2, (qRectF.top() + qRectF.bottom())/2);
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    Win32Util::mouseLeftClick();  //点击领取银票
    DeviceUtils::sendLog(widget,"点击领取银票");

}

bool RunShop::checkRunshopFill()
{
    QThread::msleep(TimeManager::getInstance()->getDelay());
    if(ImgHelper::getGameMatchPoint(":/runshop/icon/runshop/runshop_fill.png", Scalar(89, 254, 254),Scalar(91, 255, 255), Setting::getInstance()->getImgDiscern(), QRectF(0.3,0,0.7,0.4f))){
        DeviceUtils::sendLog(widget, "该角色跑商次数已满");
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return false;
        return true;
    }
    return false;
}

void RunShop::giveBackBankNotes()
{
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset + 400));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    if(!ImgHelper::getGameMatchPoint(":/runshop/icon/runshop/runshop_givebackbank.png", Scalar(14, 125, 84), Scalar(24, 175, 255), 0.6f, QRectF(0, 0.2f, 0.3f,0.8f), &qRectF) || GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL){
        return;
    }
    Win32Util::moveMouse((qRectF.left() + qRectF.right()) / 2, (qRectF.top() + qRectF.bottom())/2);
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    Win32Util::mouseLeftClick();  //点击交还银票
    DeviceUtils::sendLog(widget, "点击交还银票");
    QThread::msleep(TimeManager::getInstance()->getDelay());
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    Win32Util::keyboardClick(VK_ESCAPE);
}

bool RunShop::checkYinPiaoBig()
{
    return ImgHelper::getGameMatchPoint(":/runshop/icon/yinpiao_fill.png",  Scalar(89, 254, 254),Scalar(91, 255, 255), 0.9f, QRectF(0.3,0,0.7,0.4f));
}

bool RunShop::checkHasYinPiao()
{
    return ImgHelper::getGameMatchPoint(":/runshop/icon/runshop/no_yinpiao.png",  Scalar(89, 254, 254),Scalar(91, 255, 255), 0.9f, QRectF(0.3,0,0.7,0.4f));
}

bool RunShop::checkHasRunshopLine()
{
    return ImgHelper::getGameMatchPoint(":/runshop/icon/runshop/no_runshop_line.png",  Scalar(89, 254, 254),Scalar(91, 255, 255), 0.9f, QRectF(0.3,0,0.7,0.4f));
}

bool RunShop::checkGoodsTime(QString goodsTime, RunShopGoods *goods)
{
    int gameTime = getGameTime(); //当时辰为丑、辰、未、戌时，粮食的买价和卖价
    QStringList currentTimes = RunShopGoodsManager::getInstance()->getBestTime(gameTime);
    if(currentTimes.contains(goodsTime)){
        DeviceUtils::sendLog(widget, QString("%1时找到当前目标%2最高价").arg(gameTime).arg(goods->name));
        qDebug()<< gameTime << "gameTime=" << gameTime << ",time=" << goods->time[0] << goods->time[1] << goods->time[2] << goods->time[3];
        return true;
    }
    return false;
}

RunShop::~RunShop()
{
    if(thread != nullptr){
        delete thread;
    }
    qDebug() << "RunShop析构函数";
}

void RunShop::run()
{
    int roleTask = GameManager::getInstantce()->roleTask;
    qDebug()<< "roleTask" << roleTask;
    if(roleTask == ROLE_TASK_SHOP){
        runStatue = GameManager::RunStatue::StateGoGroup;
        goGroup();
    }else if(roleTask == ROLE_TASK_SHOP_REMOTE){
        runStatue = GameManager::RunStatue::StateLeave;
        goGroup();
    }else if(roleTask == ROLE_TASK_SHOP_BACK){
        runStatue = GameManager::RunStatue::StateBack;
        goGroup();
    }
}

void RunShop::goGroup(){
    bool isFirst = true;
    for(int i = 0; i < roleBeans->size(); i++){
        RoleBean *roleBean = roleBeans->at(i);
        if(roleBean->runshopBean == nullptr)continue;
        GameManager::getInstantce()->currentRole = roleBean;
        runshopCount = 0;
        if(i != 0){
            changeRole(roleBean->roleIndex);
        }
        while(true){
            runshopCount++;
            QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
            if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) break;
            Win32Util::keyboardClick(VK_MENU, 'V');
            DeviceUtils::sendLog(widget,QString("第%1个角色开始跑商，当前第%3次跑商").arg(i+1).arg(runshopCount));

            if(runStatue <= GameManager::RunStatue::StateGoGroup){
                if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL)break;
                runStatue = GameManager::RunStatue::StateGoGroup;
                if(goGroupCity(roleBean->runshopBean->groupCityEn,roleBean->runshopBean->groupXiaoWeiLocation , this)){
                    clickXiaoWei(roleBean->runshopBean->groupCityEn, GameManager::getInstantce()->currentRole->runshopBean->groupIndex);
                }
            }
            if(runStatue <= GameManager::RunStatue::StateGroup){
                if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL)break;
                runStatue = GameManager::RunStatue::StateGroup;
                if(runshopCount == 1){
                    goQianWeiYi();
                }else{
                    DeviceUtils::sendLog(widget,"等待钱为一的商品冷却中，请稍候。。。");
                    QThread::msleep(5000);
                    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) break;
                    clickNPC(":/runshop/icon/runshop/runshop_qianweiyi.png", false);
                }

                getBankNotes(); //领取银票
                if(checkRunshopFill()){
                    Win32Util::keyboardClick(VK_ESCAPE);
                    break;
                }
                openShop(":/runshop/icon/runshop_shop.png"); //打开商店
                buyGoods(roleBean->runshopBean, true, Setting::getInstance()->getImgDiscern()); //购买粮食
            }
            if(runStatue <= GameManager::RunStatue::StateLeave){
                if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL || roleBean->runshopBean ==nullptr)break;
                runStatue = GameManager::RunStatue::StateLeave;
                if(GameManager::getInstantce()->roleTask == ROLE_TASK_SHOP_REMOTE && isFirst){
                    isFirst = false;
                    if(goGroupCity(roleBean->runshopBean->remoteCityEn, roleBean->runshopBean->remoteXiaoWeiLocation, this)){
                        clickXiaoWei(PCITY_LEVEL, GameManager::getInstantce()->currentRole->runshopBean->remoteCityIndex);
                    }
                }else{
                    leave();
                    if(roleBean->runshopBean->runshopType == GROUP2GROUP){
                        clickXiaoWei(PCITY_LEVEL, GameManager::getInstantce()->currentRole->runshopBean->remoteCityIndex);
                    }
                }
            }
            if(runStatue <= GameManager::RunStatue::StateRemoteCity){
                if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL || roleBean->runshopBean ==nullptr)break;
                runStatue = GameManager::RunStatue::StateRemoteCity;
                if(roleBean->runshopBean->runshopType == GROUP2GROUP){
                    goQianWeiYi();
                    openShop(":/runshop/icon/runshop_shop.png");
                    remoteSell(":/runshop/icon/runshop_shop.png"); //出售粮食
                    buyGoods(roleBean->runshopBean,false, Setting::getInstance()->getImgDiscern()); //购买

                }else{
                    goRemoteMerchant();
                    openShop(":/runshop/icon/runshop/runshop_open.png");
                    remoteSell(":/runshop/icon/runshop/runshop_open.png");
                    buyCity(roleBean->runshopBean->remoteBuyShopImg, roleBean->runshopBean->remoteStartScalar, roleBean->runshopBean->remoteEndScalar, 0.8f); //买东西
                }
            }
            if(runStatue <= GameManager::RunStatue::StateBack){
                if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL || roleBean->runshopBean == nullptr )break;
                runStatue = GameManager::RunStatue::StateBack;
                if(GameManager::getInstantce()->roleTask == ROLE_TASK_SHOP_BACK && isFirst){
                    isFirst = false;
                    if(goGroupCity(roleBean->runshopBean->groupCityEn,roleBean->runshopBean->groupXiaoWeiLocation, this)){
                        clickXiaoWei(roleBean->runshopBean->groupCityEn, GameManager::getInstantce()->currentRole->runshopBean->groupIndex);
                    }
                } else {
                    back();
                    clickXiaoWei(roleBean->runshopBean->groupCityEn, GameManager::getInstantce()->currentRole->runshopBean->groupIndex);
                }
            }

            if(runStatue <= GameManager::RunStatue::StateTurn){
                if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL)break;
                runStatue = GameManager::RunStatue::StateTurn;
                goQianWeiYi();
                openShop(":/runshop/icon/runshop_shop.png"); //打开商店
                groupSell(":/runshop/icon/runshop_shop.png"); //出售
                giveBackBankNotes(); //交还银票
            }
            runStatue = GameManager::RunStatue::StateGoGroup;
            if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) break;
        }
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) break;
        runStatue = GameManager::RunStatue::StateGoGroup;
    }
    GameManager::getInstantce()->roleTask = ROLE_TASK_NORMAL;
    emit finish();
}

void RunShop::checkMove()
{
    int count = 0;
    while(true){
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_SHOP || GameManager::getInstantce()->roleTask == ROLE_TASK_SHOP_BACK || GameManager::getInstantce()->roleTask == ROLE_TASK_SHOP_REMOTE){
            qDebug() << "checkMove=" << GameManager::getInstantce()->isNeedCheckMove<< "," << !GameManager::getInstantce()->isMove;
//            && (runStatue == GameManager::RunStatue::StateLeave ||  runStatue == GameManager::RunStatue::StateBack ||runStatue == GameManager::RunStatue::StateGoGroup
            if(GameManager::getInstantce()->isNeedCheckMove && !GameManager::getInstantce()->isMove){
                if(checkCityNode != nullptr) {
                    if(count == 0){
                        count++;
                     }else{
                        count = 0;
                        DeviceUtils::sendLog(widget, "检测到角色移动中断，重新前往目的地");
                        qDebug() << "checkMove44, x=" << checkCityNode->x << "y=" << checkCityNode->y;
                        GameManager::getInstantce()->isNeedCheckMove = false;
                        moveRoles(checkCityNode, this, false);
                     }
                }
            }
        }
        QThread::msleep(2000);
    }
     qDebug() << "end";
}



